/**
 * 
 */
package dt.inlee.system.dao;

import java.util.Date;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import dt.inlee.common.dao.impl.BaseDao;
import dt.inlee.common.exception.FrameworkException;
import dt.inlee.common.util.BaseUtil;
import dt.inlee.domain.SysDictionary;

/**
 * @功能：系统字典数据访问层
 * @作者：印鲜刚
 * @创建日期：2010-04-20
 */
@Repository("sysDictionaryDao")
public class SysDictionaryDao extends BaseDao {

	/**
	 * 根据条件获取系统参数总记录数
	 * @param cnName
	 * @param name
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public int getSysDictionaryTotalRows(String cnName, String name) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysDictionary.class);
		if (!BaseUtil.isEmpty(cnName))
			criteria.add(Restrictions.like("cnName", "%" + cnName + "%"));
		if (!BaseUtil.isEmpty(name))
			criteria.add(Restrictions.like("name", "%" + name + "%"));
		criteria.setProjection(Projections.countDistinct("id"));
		List<Integer> count = criteria.list();
		return count.get(0).intValue();
	}

	/**
	 * 根据条件获取系统参数记录
	 * @param cnName
	 * @param name
	 * @param curPage
	 * @param pageSize
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public List<SysDictionary> findAllSysDictionaries(String cnName, String name, int curPage, int pageSize)
			throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysDictionary.class);
		if (!BaseUtil.isEmpty(cnName))
			criteria.add(Restrictions.like("cnName", "%" + cnName + "%"));
		if (!BaseUtil.isEmpty(name))
			criteria.add(Restrictions.like("name", "%" + name + "%"));
		if (curPage > 0 && pageSize > 0) {
			criteria.setFirstResult((curPage - 1) * pageSize);
			criteria.setMaxResults(pageSize);
		}
		return criteria.list();
	}

	/**
	 * 获取数据库时间
	 * @return
	 */
	public Date getDBTime() throws FrameworkException {
		Query sqlQuery = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery(
				"SELECT CURRENT_TIMESTAMP() FROM SysDictionary");
		Date currentTime = (Date) sqlQuery.list().get(0);
		return currentTime;
	}

	/**
	 * 根据中英名获取系统字典
	 * @param name
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysDictionary findSysDictionaryByName(String name, String cnName) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysDictionary.class);
		criteria.add(Restrictions.or(Restrictions.eq("name", name), Restrictions.eq("cnName", cnName)));
		List<SysDictionary> sysDictionaries = criteria.list();
		if (!BaseUtil.isEmpty(sysDictionaries) && sysDictionaries.size() > 0)
			return sysDictionaries.get(0);
		return null;
	}

	/**
	 * 通过系统字典ID、英文名称和中文名称查找系统字典
	 * @param id
	 * @param name
	 * @param cnName
	 * @return
	 * @throws FrameworkException
	 */
	@SuppressWarnings("unchecked")
	public SysDictionary findSysDictionaryByIdAndName(String id, String name, String cnName) throws FrameworkException {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(
				SysDictionary.class);
		criteria.add(Restrictions.ne("id", id));
		criteria.add(Restrictions.or(Restrictions.eq("name", name), Restrictions.eq("cnName", cnName)));
		List<SysDictionary> sysDictionaries = criteria.list();
		if (!BaseUtil.isEmpty(sysDictionaries) && sysDictionaries.size() > 0)
			return sysDictionaries.get(0);
		return null;
	}
}
